import { ApiProperty, getSchemaPath, ApiExtraModels } from '@nestjs/swagger';
import { CrudValidationGroups } from '@nestjsx/crud';
import {
    IsNumber,
    IsOptional,
    IsString, ValidateNested
} from 'class-validator';
import {
    Column,
    Entity,
    ManyToOne,
    OneToMany,
    PrimaryGeneratedColumn
} from 'typeorm';
import { file } from './about/file';
import { AssetsType } from './assets-type.entity';
import { Assets } from './assets.entity';
import { BaseEntity } from './base.entity';
import { Feedback } from './feedback.entity';
import { Funds } from './funds.entity';
import { Group } from './group.entity';
import { LearningResources } from './learning-resources.entity';
import { Migrate } from './migrate.entity';
import { OrgActionType } from './org-action-type.entity';
import { OrgAction } from './org-action.entity';
import { PartyMember } from './party-member.entity';
import { ProjectConstruction } from './project-construction.entity';
import { ResourcesType } from './resources-type.entity';
import { Resources } from './resources.entity';
import { SubsidyInfo } from './subsidy-info.entity';
import { SubsidyType } from './subsidy-type.entity';
import { Subsidy } from './subsidy.entity';
import { Town } from './town.entity';
import { User } from './user.entity';
import { VillageActionType } from './village-action-type.entity';
import { VillageAction } from './village-action.entity';
import { VillageBulletinBoard } from './village-bulletin-board.entity';
import { VillageCadre } from './village-cadre.entity';
import { VillageMeeting } from './village-meeting.entity';
import { Villager } from './villager.entity';

const { CREATE, UPDATE } = CrudValidationGroups;

@Entity()
@ApiExtraModels(Village)
export class Village extends BaseEntity {
  @ApiProperty({
    description: '村 -主键',
    required: false,
  })
  @PrimaryGeneratedColumn()
  id?: number;

  @ApiProperty({
    example: 1,
    description: '所属镇',
    required: true,
    oneOf: [
      {
        type: 'Number',
      },
      {
        $ref: getSchemaPath(() => Town),
      },
    ],
  })
  @ManyToOne(
    () => Town,
    Town => Town.village,
    {
      eager: true,
    },
  )
  town: Number | Town;

  @ApiProperty({
    example: '刘砦村',
    description: '行政村名字',
    required: true,
    type: String
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  name: string;

  @ApiProperty({
    example: '村所在位置',
    description: '行政村所在位置',
    required: true,
    type: String
  })
  @IsString({ always: true })
  @Column({
    type: "text",
  })
  address: string;

  @ApiProperty({
    example: '人口数',
    description: '行政村人口数',
    required: true,
    type: Number
  })
  @IsNumber({}, { always: true })
  @IsOptional({ always: true })
  @Column({
    nullable: true,
  })
  population?: number;

  @ApiProperty({
    example: '占地面积',
    description: '单位 平方m',
    required: true,
    type: String
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  footprint: string;

  @ApiProperty({
    example: '村简介',
    description: '我这个村有什么特点',
    required: true,
    type: String
  })
  @IsString({ always: true })
  @Column({
    type: "text",
  })
  introduction: string;

  @ApiProperty({
    example: '村貌图',
    description: '村貌图 图片地址',
    required: false,
    type: file
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  image?: file [];

  @ApiProperty({
    example: 1,
    description: '组',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Group,
    Group => Group.village,
  )
  group?: Group[];

  @ApiProperty({
    example: 1,
    description: '村干部',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => VillageCadre,
    VillageCadre => VillageCadre.village,
  )
  villageCadre?: VillageCadre [];

  @ApiProperty({
    example: 1,
    description: '村民账号信息',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Villager,
    Villager => Villager.village,
  )
  villager?: Villager[];

  @ApiProperty({
    example: 1,
    description: '党员',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => PartyMember,
    PartyMember => PartyMember.village,
  )
  partyMember?: PartyMember[];

  @ApiProperty({
    example: 1,
    description: '补助项',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Subsidy,
    Subsidy => Subsidy.village,
  )
  subsidy?: Subsidy[];

  @ApiProperty({
    example: 1,
    description: '补助项类型',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => SubsidyType,
    SubsidyType => SubsidyType.village,
  )
  subsidyType?: SubsidyType[];

  @ApiProperty({
    example: 1,
    description: '补助信息',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => SubsidyInfo,
    SubsidyInfo => SubsidyInfo.village,
  )
  subsidyInfo?: SubsidyInfo[];

  @ApiProperty({
    example: 1,
    description: '迁移信息',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Migrate,
    Migrate => Migrate.village,
  )
  migrate?: Migrate[];

  @ApiProperty({
    example: 1,
    description: '资源',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Resources,
    Resources => Resources.village,
  )
  resources?: Resources[];

  @ApiProperty({
    example: 1,
    description: '资源类型',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => ResourcesType,
    ResourcesType => ResourcesType.village,
  )
  resourcesType?: ResourcesType[];

  @ApiProperty({
    example: 1,
    description: '资产',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Assets,
    Assets => Assets.village,
  )
  assets?: Assets[];

  @ApiProperty({
    example: 1,
    description: '资产类型',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => AssetsType,
    AssetsType => AssetsType.village,
  )
  assetsType?: AssetsType[];

  @ApiProperty({
    example: 1,
    description: '资金',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Funds,
    Funds => Funds.village,
  )
  funds?: Funds[];

  @ApiProperty({
    example: 1,
    description: '项目建设信息',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => ProjectConstruction,
    ProjectConstruction => ProjectConstruction.village,
  )
  projectConstruction?: ProjectConstruction[];

  @ApiProperty({
    example: 1,
    description: '村告栏',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => VillageBulletinBoard,
    VillageBulletinBoard => VillageBulletinBoard.village,
  )
  villageBulletinBoard?: VillageBulletinBoard[];

  @ApiProperty({
    example: 1,
    description: '村会议',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => VillageMeeting,
    VillageMeeting => VillageMeeting.village,
  )
  villageMeeting?: VillageMeeting[];

  @ApiProperty({
    example: 1,
    description: '村组织活动',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => VillageAction,
    VillageAction => VillageAction.village,
  )
  villageAction?: VillageAction[];

  @ApiProperty({
    example: 1,
    description: '村组织活动类型',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => VillageActionType,
    VillageActionType => VillageActionType.village,
  )
  villageActionType?: VillageActionType;

  @ApiProperty({
    example: 1,
    description: '党组织活动',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => OrgAction,
    OrgAction => OrgAction.village,
  )
  orgAction?: OrgAction[];

  @ApiProperty({
    example: 1,
    description: '党组织活动类型',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => OrgActionType,
    OrgActionType => OrgActionType.village,
  )
  orgActionType?: OrgActionType[];

  @ApiProperty({
    example: 1,
    description: '反馈信息',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => Feedback,
    Feedback => Feedback.village,
  )
  feedback?: Feedback[];

  @ApiProperty({
    example: 1,
    description: '村级管理员',
    required: false,
    type: Number
  })
  @OneToMany(() => User, User => User.village)
  users : User []

  @ApiProperty({
    example: 1,
    description: '学习资源',
    required: false,
    type: Number,
  })
  @OneToMany(
    () => LearningResources,
    LearningResources => LearningResources.village,
  )
  learningResources?: LearningResources[];
}
